<template>
	<view>
		
		<view class="">
				<view class="mar_bar">
					<view class="up-box un-file" @tap="onUpload" v-if="upload_btn">
						<view class="up-bor down_btn">
							<view class="up_flex ">
								<view><image src="../../static/upload1.png" mode="" class="gou"></image></view>
								<text class="down_text">你可以选择拍照或从相册中选择</text>
								<button>下一步</button>
                <view style="margin-top: 40rpx;">提示：使用“网帮云笔”电脑端，效果更佳。</view>
							</view>
						</view>
					</view>
					
					<view class="up-box mar_top pad_bom" v-show="upload_progress">
						<view class="up-bor uping">	
							<!-- <view class="up_flex ">
								<cmd-progress type="circle" :percent="percent" :stroke-width="12" :width="150"></cmd-progress>
							</view> -->
							<view class="loader-inner ball-spin-fade-loader">
								<div></div>
								<div></div>
								<div></div>
								<div></div>
								<div></div>
								<div></div>
								<div></div>
								<div></div>
							</view>
							<text>正在处理中...</text>
						</view>
					</view>
					<view class="up-box down_btn mar_top" v-show="upload_down">
						<view class="up-bor">	
							<view class="up_flex  down_view">
								<view><image src="../../static/gou.png" mode="" class="gou"></image></view>
								<!-- #ifdef MP-WEIXIN -->
								<text class="down_text">请复制链接下载或分享</text>
								<button @tap="shareUrl">复制下载链接</button>
								<!-- #endif -->
								<!-- #ifdef H5 -->
								<text class="down_text">转换完成已自动保存至文件库</text>
								<button @tap="onDownload">{{down_btn}}</button>
								<button @tap="shareUrl">分享下载链接</button>
								<!-- #endif -->
								<!-- #ifdef APP-PLUS -->
								<text class="down_text">转换完成已自动保存至文件库</text>
								<button @tap="openPage">打开本地文件库</button>
								<!-- #endif -->
							</view>
						</view>
					</view>
					<view v-if="ocr_content" class="">
						
						<textarea value="" class="txtret" auto-height="true" v-model="content" placeholder="" />
						<div style="display: flex;justify-content: space-between;">
              <button class="copyBtn" size="mini" type="default" style="background-color: #1E9FFF;color: #fff;" @click="copy">复制</button>
       
            </div>
					</view>
					<view class="tishi mar_bar" v-if="anquan_msg">
						<text>文档安全</text>
						<text>文件上传至安全服务器，转换后会自动保存至文件库中，您可以在文件库中分享或删除。</text>
					</view>
				</view>
		</view>
	</view>
</template>

<script>

	export default {
		components:{
			
		},
		onPullDownRefresh(){
			console.log('下拉刷新');
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		data() {
			return {
				currentPage:'../../pages/upload/uploadInfo',
				xzgs:'',
				type:'',
				name:'',
				upload_btn:true,
				upload_progress:false,
				bannerImg:false,
				upload_down:false,
				percent:0,
				qrhb_btn:false,
				srgeshi:[],
				bannerImgs:[],
				anquan_msg:false,
				checkDown_status:false,
				y_filename:'',
				app_files:[],
				app_inum:0,
				app_progress:false,
				app_timer:0,
				down_timer:0,
				back_status:false,
				
				timer:null,
				filecode:null,
				down_btn:'下载',
				downpath:[],
				down_progress:false,
				select:'',
				ocr_content:false,
				content:'32131',
				img_detected:'',
				userId:'',
				userType:'',
				deviceId:'',
				sycs:1000,
        ocrname:'',
        urls:[],
        userFile:''
			}
		},
		onLoad:function(e){
			// this.checkCache();
			this.checkSycs();
			this.isLogin();
			this.xzgs = e.xzgs;
			this.type = e.type;
			this.name = e.name;
			if(this.xzgs==''||this.type=='')uni.reLaunch({url:'../index/index'})
			console.log(e);
			
			
		},
		onBackPress(event) {
			console.log('页面返回',event);
			// #ifdef APP-PLUS
			var that = this;
			if(that.back_status ==false){
				that.back_status = true;
				if(that.down_progress==true){
					uni.showToast({
						icon: 'none',
						title: '当前有文件正在下载,请勿退出该软件,稍候前往文件库中查看'
					});
					return true;
				}
			}
			
			// #endif
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				try {
					const value = uni.getStorageSync('userInfo');
					if (value) {
						this.userId = value['user_id'];
						this.userType = value['user_type'];
					}else{
						this.userId = '';
						console.log('用户未登录');
					}
				} catch (e) {
					// error
				}
			},
			copy(){
			  //提示模板
			  uni.showModal({
			    content:this.content,//模板中提示的内容
			    confirmText:"复制内容",
          showCancel:false,
			    success:()=>{//点击复制内容的后调函数
			      //uni.setClipboardData方法就是讲内容复制到粘贴板
			      uni.setClipboardData({
			        data:this.content,//要被复制的内容
			        success:()=>{//复制成功的回调函数
			          uni.showToast({//提示
			            title:"复制成功"
			          })
			        }
			      });
			    }
			  });
			},
     
			onUpload(){
				uni.chooseImage({
				    count: 9, //默认9
				    success: (res) => {
              let arr = res.tempFilePaths[0]
              
              let rename = arr.substring(0,arr.length-4)
              console.log(arr);
              console.log(rename);
              console.log(res.tempFiles);
              
              
            if(res.tempFiles.length == 1) {
              // this.onepage()
              this.userFile = res.tempFilePaths[0]
              console.log(this.userFile);
      
                uni.uploadFile({
                                   	url: getApp().globalData.apiDomain+'upload/file', //仅为示例，非真实的接口地址
                                     header: {
                                         'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
                                     	'token':uni.getStorageSync('wbyun_token'),
                                       'wbapp':'weixin_mp_xbgy'
                                     },
                                   	filePath: this.userFile,
                                   	name: 'file', 
                                   	formData: {
                                   		'ftype':'img2docx_post',
                                      'app':'yes'
                                   	},
                                   	success: (uploadFileRes) => {
                                   	   console.log(uploadFileRes) 
                                   	   let data = JSON.parse(uploadFileRes.data)
                                   	   console.log(data);
                                   	   if(data.code == 200) {
                                         console.log(data.datas.url);
                                         this.urls.push(data.datas.url)
                                          console.log(this.urls);
                                          
                                          uni.showLoading({
                                            title:'转换中...',
                                            mask:true
                                          })
                                   
                                   	     let timer = setInterval(() => {
                                   	       uni.request({
                                   	       	url: getApp().globalData.apiDomain+'convert/img2doc_post',
                                   	       	method: 'POST',
                                   	       	header: {
                                   	       		'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
                                   	       		'token': uni.getStorageSync('wbyun_token'),
                                              'wbapp':'weixin_mp_xbgy'
                                   	       	},
                                   	       	data: {
                                              'urls': JSON.stringify(this.urls)
                                            },
                                   	       	dataType: 'json',
                                   	       	success: res => {
                                   	       		console.log(res);
                                   	          if(res.data.code == 200) {
                                   	           let taskid = res.data.datas.taskid
                                                 this.finallytran(taskid)
                                                 clearInterval(timer)
                                            
                                   	        
                              
                                   	                        	 
                                   	          }else if(res.data.code == 300) {
                                   	            
                                   	          }else if(res.data.code == 519) {
                                                uni.showToast({
                                                  	title: res.data.msg,
                                                  	icon: 'none'
                                                });
                                                setTimeout(() => {
                                                  uni.navigateTo({
                                                    url:'/pagesB/subson/subson/apply'
                                                  })
                                                },2000)
                                                clearInterval(timer)
                                              }else {
                                   	            uni.showToast({
                                   	              	title: '文件处理失败请稍后再试!',
                                   	              	icon: 'none'
                                   	            });
                                   	            clearInterval(timer)
                                   	          }
                                   	       
                                   	       	},
                                             fail: () => {},
                                             complete: () => {
                                             }
                                   	       	
                                   	       });
                                   	     },1500)
                                   	                        	 
                                   	   }else {
                                         uni.hideLoading()
                                         uni.showToast({
                                           	title: data.msg,
                                           	icon: 'none'
                                         });
                                       }
                                   	}
                                   });
              
              
              
            }else if(res.tempFiles.length > 1){
              uni.showLoading({
                 title:'转换中...',
                 mask:true
               })
              let arrs = []
              res.tempFiles.map(item => {
                arrs.push(item.path)
              })
              console.log(arrs);
              this.morepage(arrs)
            }
			
				
			
				
            
            
				    }
				});
			},
      onepage() {
        
      },
      morepage(e) {
        console.log(e);
        
        uni.uploadFile({
                    	url: getApp().globalData.apiDomain+'upload/file', //仅为示例，非真实的接口地址
                      header: {
                          'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
                      	'token':uni.getStorageSync('wbyun_token'),
                        'wbapp':'weixin_mp_xbgy'
                      },
                    	filePath: e[0],
                    	name: 'file', 
                    	formData: {
                    		'ftype':'img2docx_post',
                       'app':'yes'
                    	},
                    	success: (uploadFileRes) => {
                    	   console.log(uploadFileRes) 
                    	   let data = JSON.parse(uploadFileRes.data)
                    	   console.log(data);
                    	   if(data.code == 200) {
                           e.shift()
                           this.urls.push(data.datas.url)
                           if(e.length !== 0) {
                             console.log(e,'998');
                             this.morepage(e)
                           }else {
                             console.log('0000');
                             console.log(this.urls);
                             let timer = setInterval(() => {
                               uni.request({
                               	url: getApp().globalData.apiDomain+'convert/img2doc_post',
                               	method: 'POST',
                               	header: {
                               		'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
                               		'token': uni.getStorageSync('wbyun_token'),
                                 'wbapp':'weixin_mp_xbgy'
                               	},
                               	data: {
                                 'urls': JSON.stringify(this.urls)
                               },
                               	dataType: 'json',
                               	success: res => {
                               		console.log(res);
                                  if(res.data.code == 200) {
                                   let taskid = res.data.datas.taskid
                                    this.finallytran(taskid)
                                    clearInterval(timer)      	 
                                  }else if(res.data.code == 300) {
                                    
                                  }else if(res.data.code == 519) {
                                                uni.showToast({
                                                  	title: res.data.msg,
                                                  	icon: 'none'
                                                });
                                                setTimeout(() => {
                                                  uni.navigateTo({
                                                    url:'/pagesB/subson/subson/apply'
                                                  })
                                                },2000)
                                                clearInterval(timer)
                                              }else {
                                    uni.showToast({
                                      	title: '文件处理失败请稍后再试!',
                                      	icon: 'none'
                                    });
                                    clearInterval(timer)
                                  }
                               
                               	},
                                fail: () => {},
                                complete: () => {
                                }
                               	
                               });
                             },1500)
                           }
                        
                    
                    	     
                    	                        	 
                    	   }else {
                          uni.hideLoading()
                          uni.showToast({
                            	title: data.msg,
                            	icon: 'none'
                          });
                        }
                    	}
                    });
      },
      finallytran(taskid) {
                     setTimeout(()=> {
                       uni.request({
                          	url: getApp().globalData.apiDomain+'convert/checkdown',
                          	method: 'POST',
                          	header: {
                          		'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
                          		'token': uni.getStorageSync('wbyun_token'),
                              'wbapp':'weixin_mp_xbgy'
                          	},
                          	data: {
                              'taskid':taskid,
                            },
                          	dataType: 'json',
                          	success:  res => {
                          		if (res['data']['code'] == 200) {
                               console.log(res.data.datas.urls);
                               let url = res.data.datas.urls
                               
                               uni.hideLoading()
                               uni.showToast({
                                 	title: '处理成功',
                                 	icon: 'none'
                               });
                                uni.downloadFile({
                                  url: url, // 文件下载地址
                                  success: response => {
                                    if (response.statusCode === 200) {
                                      uni.getFileSystemManager().saveFile({
                                        tempFilePath: response.tempFilePath,
                                        success: (resData) => {
                                          uni.openDocument({
                                            filePath: resData.savedFilePath,
                                            showMenu: true, // 允许出现分享功能
                                            success: r => {
                                              console.log(r)
                                          
                                            },
                                            fail: openError => {
                                              console.log('打开失败: ', openError)
                                            }
                                          })
                                        },
                                        fail: error => {
                                          console.log('error: ', error)
                                        }
                                      })
                                    }
                                  }
                                })
                               
                               
                          			return;   
                          		}else if(res['data']['code'] == '300') {
                                this.finallytran(taskid)
                              } else { 
                          			uni.showToast({
                          				title: res['data']['msg'],
                          				icon: 'none'
                          			});
                          		}
                          	},
                          	fail: () => {},
                          	complete: () => {
                          	}
                          });
                     },1500)
                  },
      
			openPage(){
				
			},
			checkSycs(){
				try {
				    const value = uni.getStorageSync('systemInfo');
					this.deviceId = value['deviceId'];
				    if (value) {
				        console.log(value);
						uni.request({
							url: 'https://cn.any2anyconverteronline.com/?XzMAPI/checkDeviceSycs',
							method: 'POST',
							data: value,
							header: {
							    'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
							},
							success: res => {
								this.sycs = res['data']['data'];
							},
							fail: () => {},
							complete: () => {}
						});
				    }
				} catch (e) {
				    console.log(e,'upload storage error');
				}
			}
		}
	}
</script>

<style>
page{background-color: #FFFFFF;}
	view {
		font-size: 14px;
		line-height: inherit;
	}
	.swiper{
		/* margin-top: 49px; */
		width: 750rpx;
		height: 372rpx;
	}
	uni-swiper {
		/* margin-top: 49px; */
		width: 750rpx;
		height: 372rpx;
	}
	.swiper image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
	.nav-head{position: fixed;width: 100%;z-index: 9;}
	.c-cont{padding: 20rpx 25rpx;}
	.title{margin-bottom: 10rpx;}
	.update image{height: 159rpx;width: 231rpx;}
	.up-box{background-color: #fff;margin: .5em 0;}
	.up-bor{padding: 0 1em;}
	.up-cont{border: 1rpx dashed #1487eb;height: 450rpx;margin: 20rpx 0;}
	.up_flex{display: flex;flex-direction: column;justify-content: center;align-items: center;}
	.ban-img{
		/* padding-top: 3.4em; */
		width: 750rpx;
		height: 372rpx;}
	.mini-btn{margin:2em 0 ; padding: 0 3em;background-color: #007aff !important;}
	uni-image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
	.about-text{background-color: #ffffff;padding: .5em 1em;margin:1em 0 4em 0;}
	.about-text text{display: inline-block;line-height: 1.3em;}
	uni-tabbar{display: block !important}
	/* #ifdef H5 */
	.uni-flex{flex-direction: row;flex-wrap: wrap;justify-content: end;} 
/* 	@media  (min-width: 320px)  and (max-width: 375px) {
		.uni-flex{flex-direction: row;flex-wrap: wrap;justify-content: space-around;} 
	} */
	.mar_bar{margin-bottom: 140rpx;}
	/* #endif */
	/* 进度条 */
	.cmd-progress-inner{width: 160rpx;height: 160rpx;}
	.down_btn{margin: 0 auto;position: relative;}
	.down_btn button{
		/* position: absolute;bottom: 80rpx;left: 24%; */
		background-color: #1487eb;color: #FFFFFF;width: 250px;font-size: 1.2em;}
	.down_text{margin: 30rpx 0 20rpx 0;}
	.gou{width: 100rpx;height: 100rpx;}
	.down_view button{margin: 20rpx 0 10rpx 0;}
	.down_view button:last-child{background-color: #FFFFFF;color: #1487eb;}
	/* .down_view button:last-child::after{border: 1rpx solid #044ff7;color: #1487eb;} */
	/* 加密 */
	.change_pwd{padding: 0 32rpx;}
	.change_pwd text{font-size: 1.273em;}
	.box_pwd{margin-top: 20rpx;}
	.place_style{border: 1rpx solid #555555;height: 1.7em;line-height: 1.7em;font-size: 1.3em;padding-left: 10rpx;color: #929292;margin: 17rpx 0;}
	/* 合并 */
	.list_hb{display: inline-block;
    width: 98%;
    overflow: hidden;border-bottom: 1rpx solid #ececec;padding: 10rpx 0;}
	.ban-img image{width: 100%;height: 100%;}
	.hb_btn{position: fixed;bottom: 80rpx;left: 120rpx;background-color: rgba(20,135,235,.48) !important;}
	/* .down_btn .hb_btn{position: absolute;top: -20rpx;right: 50rpx;width: 120rpx !important;z-index: 99999;font-size: 1em;padding: 0;line-height: initial;} */
	
	/* 选择文件 */
	.un-file{margin: 2.5em 0;}
	.un-file .down_text{margin: 1.5em 0;}
	/* 上传中 */
	.uping{text-align: center;}
	/* 底部文字 */
	.tishi{position: fixed;bottom: 0;text-align: center;padding: 0 60rpx;color: #999999;font-size: 1em;}
	.tishi text{display: block;}
	.tishi text:last-child{margin-top: 20rpx;line-height: 40rpx;}
	.mar_bar{margin-bottom: 140rpx;}
	.flex_pwd{display: flex;}
	.grid-cell{flex: 1;display: inline-block;}
	.grid-cell:last-child{padding-top: 20rpx;}
	.checklist-group{justify-content: space-between;}
	.pad_bom{padding:3em 0 0 0 ;}
	.mar_top{margin-top: 120rpx;}
	
	
	
	/* 添加转换动画 */
	@-webkit-keyframes ball-spin-fade-loader {
	  50% {
	    opacity: 0.3;
	    -webkit-transform: scale(0.4);
	            transform: scale(0.4); }
	
	  100% {
	    opacity: 1;
	    -webkit-transform: scale(1);
	            transform: scale(1); } }
	
	@keyframes ball-spin-fade-loader {
	  50% {
	    opacity: 0.3;
	    -webkit-transform: scale(0.4);
	            transform: scale(0.4); }
	
	  100% {
	    opacity: 1;
	    -webkit-transform: scale(1);
	            transform: scale(1); } }
	
	.ball-spin-fade-loader {
	  position: relative; }
	  .ball-spin-fade-loader > div:nth-child(1) {
	    top: 25px;
	    left: 0;
	    -webkit-animation: ball-spin-fade-loader 1s 0s infinite linear;
	            animation: ball-spin-fade-loader 1s 0s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(2) {
	    top: 17.04545px;
	    left: 17.04545px;
	    -webkit-animation: ball-spin-fade-loader 1s 0.12s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.12s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(3) {
	    top: 0;
	    left: 25px;
	    -webkit-animation: ball-spin-fade-loader 1s 0.24s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.24s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(4) {
	    top: -17.04545px;
	    left: 17.04545px;
	    -webkit-animation: ball-spin-fade-loader 1s 0.36s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.36s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(5) {
	    top: -25px;
	    left: 0;
	    -webkit-animation: ball-spin-fade-loader 1s 0.48s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.48s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(6) {
	    top: -17.04545px;
	    left: -17.04545px;
	    -webkit-animation: ball-spin-fade-loader 1s 0.6s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.6s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(7) {
	    top: 0;
	    left: -25px;
	    -webkit-animation: ball-spin-fade-loader 1s 0.72s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.72s infinite linear; }
	  .ball-spin-fade-loader > div:nth-child(8) {
	    top: 17.04545px;
	    left: -17.04545px;
	    -webkit-animation: ball-spin-fade-loader 1s 0.84s infinite linear;
	            animation: ball-spin-fade-loader 1s 0.84s infinite linear; }
	  .ball-spin-fade-loader > div {
	    background-color: #0055ff;
	    width: 15px;
	    height: 15px;
	    border-radius: 100%;
	    margin: 2px;
	    -webkit-animation-fill-mode: both;
	            animation-fill-mode: both;
	    position: absolute; }
		
		.ball-spin-fade-loader{width: 1%;height: 120rpx;margin: 0 auto;transform:scale(1.2,1.2);padding-right: 35rpx;}
		
		/* ocr */
		.up-bor textarea{
			width: auto;
			padding: 10rpx;    
			border-radius:10rpx;
			border: 1px solid #cccccc;
		}
		.copyBtn{margin-top: 50rpx;
    width: 300rpx;}
    .txtret {
          width: 95%;
          height: 700rpx !important;
          margin: auto;
          margin-top: 50rpx;
          padding: 10rpx;
          background: #f7f7f7;
    }
</style>
